package com.jinniulive.platform.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.CodeInfo;
import com.jinniulive.platform.bean.RegisterInfo;
import com.jinniulive.platform.tools.TelNumMatch;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class EnrollActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {

    private EditText mEditTextPhone,mEditTextCode;
    private TextView mTextViewGetCode,mTextViewAgreement;
    private Button mButtonAgreement,mButtonNext;
    private String license;
    private Boolean isAgreement = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enroll);
        initView();
        setListener();



    }

    private void setListener() {
        mTextViewGetCode.setOnClickListener(this);
        mEditTextCode.addTextChangedListener(this);
        mEditTextPhone.addTextChangedListener(this);



    }

    private void initView() {
        mEditTextPhone = (EditText) findViewById(R.id.activity_enroll_et_phone);
        mEditTextCode = (EditText) findViewById(R.id.activity_enroll_et_code);
        mTextViewGetCode = (TextView) findViewById(R.id.activity_enroll_tv_getcode);
        mTextViewAgreement = (TextView) findViewById(R.id.activity_enroll_tv_xieyi);
        mButtonAgreement = (Button) findViewById(R.id.activity_enroll_bt_agreement);
        mButtonNext = (Button) findViewById(R.id.activity_enroll_bt_next);
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()){
                //退出
                case R.id.activity_enroll_back:
                    finish();
                    break;
                //获取验证码
                case R.id.activity_enroll_tv_getcode:
                    final String phoneNumber = mEditTextPhone.getText().toString().trim();
                    if (phoneNumber==null || phoneNumber.isEmpty()){
                        Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    }else if (!TelNumMatch.isValidPhoneNumber(phoneNumber)){
                        Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                    }else{

                        String format = String.format(NetConfig.ZCYZ, phoneNumber);
                        RequestParams params=new RequestParams(format);
                        x.http().get(params, new Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                Gson gson=new Gson();
                                RegisterInfo registerInfo = gson.fromJson(result, RegisterInfo.class);
                                int errorcode = registerInfo.getErrorcode();
                                final String codedes = registerInfo.getCodedes();
                                license = registerInfo.getLicense();
                                if (errorcode == 200){
                                    Toast.makeText(EnrollActivity.this, "正在发送", Toast.LENGTH_SHORT).show();
                                    String format1 = String.format(NetConfig.YZMQQ, phoneNumber,"reg", license);
                                    RequestParams params1=new RequestParams(format1);
                                    x.http().get(params1, new CommonCallback<String>() {
                                        @Override
                                        public void onSuccess(String result) {
                                           Gson gson = new Gson();
                                            CodeInfo codeInfo = gson.fromJson(result, CodeInfo.class);
                                            int errorcode1 = codeInfo.getErrorcode();
                                            String codedes1 = codeInfo.getCodedes();
                                            if (errorcode1==200){
                                                CountDownTimer mTimer = new CountDownTimer(60000, 1000) {
                                                    @Override
                                                    public void onTick(long millisUntilFinished) {
                                                        mTextViewGetCode.setEnabled(false);
                                                        mTextViewGetCode.setText(String.format("剩余%d秒", millisUntilFinished / 1000));
                                                    }

                                                    @Override
                                                    public void onFinish() {
                                                        mTextViewGetCode.setEnabled(true);
                                                        mTextViewGetCode.setText("获取验证码");
                                                    }
                                                }.start();
                                                Toast.makeText(EnrollActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                                            }else{
                                                Toast.makeText(EnrollActivity.this, codedes1, Toast.LENGTH_SHORT).show();
                                            }
                                        }

                                        @Override
                                        public void onError(Throwable ex, boolean isOnCallback) {
                                            Toast.makeText(EnrollActivity.this, "验证码获取失败", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onCancelled(CancelledException cex) {

                                        }

                                        @Override
                                        public void onFinished() {

                                        }
                                    });


                                }else{
                                    Toast.makeText(EnrollActivity.this, codedes, Toast.LENGTH_SHORT).show();
                                }


                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {

                            }

                            @Override
                            public void onCancelled(CancelledException cex) {

                            }

                            @Override
                            public void onFinished() {

                            }
                        });
                    }
                    break;
                //协议是否同意
                case R.id.activity_enroll_bt_agreement:
                    if (isAgreement){
                        mButtonAgreement.setBackgroundDrawable(getResources().getDrawable(R.mipmap.zc1_03));
                        mButtonNext.setEnabled(false);
                        mButtonNext.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
                        isAgreement = false;
                    }else{
                        mButtonAgreement.setBackgroundDrawable(getResources().getDrawable(R.mipmap.zc1_04));
                        isAgreement = true;
                        if (!TextUtils.isEmpty(mEditTextCode.getText().toString().trim())){
                            mButtonNext.setEnabled(true);
                            mButtonNext.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_14));
                        }
                    }
                    break;
                // 下一步
                case R.id.activity_enroll_bt_next:
                    final String phone = mEditTextPhone.getText().toString().trim();
                    String code = mEditTextCode.getText().toString().trim();
                    String format = String.format(NetConfig.YZMYZ, phone,"reg", license, code);
                    RequestParams params=new RequestParams(format);
                    x.http().get(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            Gson gson =new Gson();
                            CodeInfo codeInfo = gson.fromJson(result, CodeInfo.class);
                            int errorcode = codeInfo.getErrorcode();
                            if (errorcode == 200){
                                Intent intent = new Intent(EnrollActivity.this, SubEnrollActivity.class);
                                intent.putExtra("PhoneNumber",phone);
                                startActivity(intent);
                                finish();
                            }else{
                                Toast.makeText(EnrollActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //mEditTextPhone 监听
        if (TextUtils.isEmpty(mEditTextPhone.getText().toString().trim())){
            mEditTextCode.setEnabled(false);
        }else {
            mEditTextCode.setEnabled(true);
        }
        //
        if (TextUtils.isEmpty(mEditTextCode.getText().toString().trim())){
            mButtonNext.setEnabled(false);
            mButtonNext.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
        }else {
            if (!isAgreement){
                mButtonNext.setEnabled(false);
                mButtonNext.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
            }else{
                mButtonNext.setEnabled(true);
                mButtonNext.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_14));
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
